package me.controller;

import me.domain.Student;
import me.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 用于学生信息的controller
 *
 * @author Rainh
 */
@Controller
public class StudentController {

    //实例化studentService对象
//    private StudentService studentService = new StudentServiceImpl();

    @Resource(name = "studentService")
    private StudentService studentService;
    /**
     * 添加学生时候
     * @return
     */
    @RequestMapping("/addController")
    public String add(Student student) {
        //执行添加学生方法
        studentService.addStudent(student);
        //跳转到前置首页
//        request.getRequestDispatcher("/indexController").forward(request,response);
//        return "forward:indexController";
        return "redirect:/indexController";
    }

    /**
     * 删除学生的时候
     */
    @RequestMapping("/deleteController/{id}")
    public String delete(@PathVariable String id) {

//        System.out.println("这里获取到的id是:"+id);
        //执行删除方法
        studentService.deleteStudentById(Long.parseLong(id));
        //跳转到前置首页
//        request.getRequestDispatcher("/indexController").forward(request,response);
//        return "forward:indexController";
        return "redirect:/indexController";
//        return;

    }

    /**
     * 修改前需要的准备工作
     * @param id
     */
    @RequestMapping("/modifyController/{id}")
    public String modify(@PathVariable String id, Model model){

        //打印一下id，以防出错
//        System.out.println("这里获取到的id是:"+id);

        //通过ID查到一个学生对象
        Student student = studentService.findStudentById(Long.parseLong(id));
//        System.out.println("要修改的学生对象是:"+student);
        //将修改的对象放入request中保存
//        request.setAttribute("STUDENT",student);
        model.addAttribute("STUDENT",student);
        //将修改对象传递给update.jsp页面
        return "update";
    }

    /**
     * 修改的方法
     * @param student
     */
    @RequestMapping("/updateController")
    public String updateController(Student student) {

        //执行修改方法
        studentService.updateStudent(student);

        //跳转到前置首页
//        request.getRequestDispatcher("/indexController").forward(request,response);
//        return "forward:indexController";
        return "redirect:/indexController";
    }

}
